package com.app.micaihu.view.home.focusnews.fragment

import android.view.View
import com.app.base.BaseMvpFragment
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.configure.LoadType
import com.app.micaihu.view.bean.TopNewsGroup
import com.app.micaihu.view.main.MainActivity
import com.app.micaihu.view.home.focusnews.adapter.FocusNewsListAdapter
import com.app.micaihu.view.home.focusnews.contract.FocusNewsContract
import com.app.micaihu.view.home.focusnews.presenter.FocusNewsPresenter
import com.app.micaihu.view.main.view.WrapContentLinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.focus_news_list.*
import kotlinx.android.synthetic.main.main_arms_list.refreshLayout
import kotlinx.android.synthetic.main.view_common_rv.*
import kotlinx.android.synthetic.main.view_common_rv.rvList

/**
 * 要闻
 */
class FocusNewsListFragment : BaseMvpFragment<FocusNewsContract.View, FocusNewsPresenter>(), FocusNewsContract.View {

    override fun getPresenter() = FocusNewsPresenter()

    override fun getLayoutId() = R.layout.focus_news_list

    //操作监听
    private var mOnRefreshingListener: MainActivity.OnRefreshingListener? = null
    private val focusNewsListAdapter: FocusNewsListAdapter by lazy {
        val focusNewsListAdapter = FocusNewsListAdapter(context)
        rvList.layoutManager = WrapContentLinearLayoutManager(context)
        rvList.adapter = focusNewsListAdapter
        focusNewsListAdapter
    }

    override fun setData() {
        refreshLayout.setEnableLoadMore(true)
        refreshLayout.setEnableAutoLoadMore(true)
        refreshLayout.setEnableLoadMoreWhenContentNotFull(false)
    }

    override fun lazyLoad() {
        mPresenter?.refresh()
    }

    override fun addListeners() {
        refreshLayout.setOnRefreshListener {
            mPresenter?.refresh()
        }
        refreshLayout.setOnLoadMoreListener {
            mPresenter?.loadMore()
        }
    }

    override fun handleTopNewsList(video: BaseBean<List<TopNewsGroup>?>?, loadType: Int) {
        viewLoading.visibility = View.GONE
        refreshLayout.finishRefresh(Config.delayRefresh)
        mOnRefreshingListener?.let {
            if (LoadType.TYPE_REFRESH_SUCCESS == loadType || LoadType.TYPE_REFRESH_ERROR == loadType) {
                it.onDataRefreshingEnd()
            }
        }
        if (video == null) {
            if (focusNewsListAdapter.groupCount > 0) {
                if (LoadType.TYPE_REFRESH_ERROR == loadType) {
                    ToastUtils.showShort(R.string.empty_network_error)
                } else {
                    refreshLayout.finishLoadMore(false)
                }
            } else {
                focusNewsListAdapter.showEmptyView(true)
            }
        } else {
            if (video.isSuccess()) {
                video.data?.let {
                    if (LoadType.TYPE_REFRESH_SUCCESS == loadType) {
                        focusNewsListAdapter.setGroups(it)
                    } else if (LoadType.TYPE_LOAD_MORE_SUCCESS == loadType) {
                        focusNewsListAdapter.addGroups(it)
                    }
                    if (it.isNullOrEmpty()) {
                        refreshLayout.finishLoadMoreWithNoMoreData()
                    } else {
                        refreshLayout.finishLoadMore(Config.delayLoadMore)
                    }
                }
            } else {
                ToastUtils.showShort(video.msg)
            }
        }
    }

    /* 供外部调用刷新数据 */
    fun flushData() {
        rvList.smoothScrollToPosition(0)
        refreshLayout.autoRefresh()
    }


    fun setOnRefreshingListener(onRefreshingListener: MainActivity.OnRefreshingListener) {
        this.mOnRefreshingListener = onRefreshingListener
    }
}